from tkinter import *

root = Tk()
root.title('计算机')
root.configure(bg='black')
root.geometry("+700+300")

label = Label(root, width=20, height=4, bg='black', fg='pink', text='0', font='Time 12 bold italic', anchor='e')

def findDot():
    s = label['text']
    reversedList = list(reversed(s))
    for i in reversedList:
        if i in ['+', '-', '*', '/']:
            return False
        elif i == '.':
            return True
        else:
            continue

def findZero():
    if len(label['text']) >=2 and label['text'][-1] == '0' and label['text'][-2] in ['+', '-', '*', '/']:
        return True
    elif label['text'] == '0':
        return True
    else:
        False


def show1():
    if label['text'] =='0':
        label['text'] = '1'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '1'
    if findZero():
        label['text'] = label['text'][:-1] + '1'
    else:
        label['text'] = label['text'] + '1'


def show2():
    if label['text'] =='0':
        label['text'] = '2'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '2'
    if findZero():
        label['text'] = label['text'][:-1] + '2'
    else:
        label['text'] = label['text'] + '2'

def show3():
    if label['text'] =='0':
        label['text'] = '3'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '3'
    if findZero():
        label['text'] = label['text'][:-1] + '3'
    else:
        label['text'] = label['text'] + '3'

def show4():
    if label['text'] =='0':
        label['text'] = '4'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '4'
    if findZero():
        label['text'] = label['text'][:-1] + '4'
    else:
        label['text'] = label['text'] + '4'

def show5():
    if label['text'] =='0':
        label['text'] = '5'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '5'
    if findZero():
        label['text'] = label['text'][:-1] + '5'
    else:
        label['text'] = label['text'] + '5'

def show6():
    if label['text'] =='0':
        label['text'] = '6'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '6'
    if findZero():
        label['text'] = label['text'][:-1] + '6'
    else:
        label['text'] = label['text'] + '6'

def show7():
    if label['text'] =='0':
        label['text'] = '7'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '7'
    if findZero():
        label['text'] = label['text'][:-1] + '7'
    else:
        label['text'] = label['text'] + '7'

def show8():
    if label['text'] =='0':

       label['text'] = '8'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '8'
    if findZero():
        label['text'] = label['text'][:-1] + '8'
    else:
        label['text'] = label['text'] + '8'

def show9():
    if label['text'] =='0':
        label['text'] = '9'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '9'
    if findZero():
        label['text'] = label['text'][:-1] + '0'
    else:
        label['text'] = label['text'] + '9'

def showDiv():
    if label['text'] and label['text'][-1] in ['+', '-', '*', '/']:
        label['text'] = label['text'][:-1] + '/'
    else:
        label['text'] = label['text'] + '/'

def showMu():
    if label['text'] and label['text'][-1] in ['+', '-', '*', '/']:
        label['text'] = label['text'][:-1] + '*'
    else:
        label['text'] = label['text'] + '*'


def showClear():
    label['text']  = ''
    label['text'] = '0'

def showSub():
    if label['text'] and label['text'][-1] in ['+', '-', '*', '/']:
        label['text'] = label['text'][:-1] + '-'
    else:
        label['text'] = label['text'] + '-'


def showDot():
    if findDot():
        pass
    else:
        label['text'] = label['text'] + '.'

def showAdd():
    if label['text'] and label['text'][-1] in ['+', '-', '*', '/']:
        label['text'] = label['text'][:-1] + '+'
    else:
        label['text'] = label['text'] + '+'

def show0():
    if label['text'] =='0':
        label['text'] ='0'
    elif len(label['text']) ==2 and label['text'][0] in ['+', '-', '*', '/'] and label['text'][1] =='0':
        label['text'] = label['text'][0] + '0'
    if findZero():
        label['text'] = label['text'][:-1] + '0'
    else:
        label['text'] = label['text'] + '0'


def showBack():
    label['text'] = label['text'][:-1]
    if label['text'] == '':
        label['text'] = '0'

def showEqual():
    if label['text'] and label['text'][-1] in ['+', '-', '*', '/']:
        result =eval(label['text'][:-1])
    else:
        result = eval(label['text'])
    label['text'] = str(result)


btn7 = Button(root,text='7',width=5, height=2, relief="groove", bg='blue',fg='white',command=show7)
btn8 = Button(root,text='8',width=5, height=2, relief="groove", bg='blue',fg='white',command=show8)
btn9 = Button(root,text='9',width=5, height=2, relief="groove", bg='blue',fg='white',command=show9)
btnDiv = Button(root,text='/',width=5, height=2, relief="groove", bg='black',fg='white',command=showDiv)
btnBack = Button(root,text='←',width=5, height=2, relief="groove", bg='blue',fg='white',command=showBack)
btnSquare = Button(root,text='²',width=5, height=2, relief="groove", bg='blue',fg='white')

btn4 = Button(root,text='4',width=5, height=2, relief="groove", bg='blue',fg='white',command=show4)
btn5 = Button(root,text='5',width=5, height=2, relief="groove", bg='blue',fg='white',command=show5)
btn6 = Button(root,text='6',width=5, height=2, relief="groove", bg='blue',fg='white',command=show6)
btnMu = Button(root,text='*',width=5, height=2, relief="groove", bg='blue',fg='white',command=showMu)
btnClear = Button(root,text='C',width=5, height=2, relief="groove", bg='blue',fg='white',command=showClear)
btnCube = Button(root,text='³',width=5, height=2, relief="groove", bg='blue',fg='white')

btn1 = Button(root,text='1',width=5, height=2, relief="groove", bg='blue',fg='white',command=show1)
btn2 = Button(root,text='2',width=5, height=2, relief="groove", bg='blue',fg='white',command=show2)
btn3 = Button(root,text='3',width=5, height=2, relief="groove", bg='black',fg='white',command=show3)
btnSub = Button(root,text='-',width=5, height=2, relief="groove", bg='blue',fg='white',command=showSub)
btnEqual = Button(root,text='=',width=5, height=2, relief="groove", bg='red',fg='white',command=showEqual)
btnRadical = Button(root,text='√',width=5, height=2, relief="groove", bg='blue',fg='white')

btn0 = Button(root,text='0',width=5, height=2, relief="groove", bg='blue',fg='white',command=show0)
btnDot = Button(root,text='.',width=5, height=2, relief="groove", bg='purple',fg='white',command=showDot)
btnAdd = Button(root,text='+',width=5, height=2, relief="groove", bg='blue',fg='white',command=showAdd)
btnSin = Button(root,text='sin',width=5, height=2, relief="groove", bg='blue',fg='white')
btnCos = Button(root,text='cos',width=5, height=2, relief="groove", bg='blue',fg='white')
btnModulus = Button(root,text='%',width=5, height=2, relief="groove", bg='blue',fg='white')
btn7 = Button(root,text='7',width=5, height=2, relief="groove", bg='pink',fg='black',command=show7)
btn8 = Button(root,text='8',width=5, height=2, relief="groove", bg='pink',fg='black',command=show8)
btn9 = Button(root,text='9',width=5, height=2, relief="groove", bg='pink',fg='black',command=show9)
btnDiv = Button(root,text='/',width=5, height=2, relief="groove", bg='pink',fg='black',command=showDiv)
btnBack = Button(root,text='←',width=5, height=2, relief="groove", bg='pink',fg='black',command=showBack)
btnSquare = Button(root,text='²',width=5, height=2, relief="groove", bg='pink',fg='black')

btn4 = Button(root,text='4',width=5, height=2, relief="groove", bg='pink',fg='black',command=show4)
btn5 = Button(root,text='5',width=5, height=2, relief="groove", bg='pink',fg='black',command=show5)
btn6 = Button(root,text='6',width=5, height=2, relief="groove", bg='pink',fg='black',command=show6)
btnMu = Button(root,text='*',width=5, height=2, relief="groove", bg='pink',fg='black',command=showMu)
btnClear = Button(root,text='C',width=5, height=2, relief="groove", bg='pink',fg='black',command=showClear)
btnCube = Button(root,text='³',width=5, height=2, relief="groove", bg='pink',fg='black')

btn1 = Button(root,text='1',width=5, height=2, relief="groove", bg='pink',fg='black',command=show1)
btn2 = Button(root,text='2',width=5, height=2, relief="groove", bg='pink',fg='black',command=show2)
btn3 = Button(root,text='3',width=5, height=2, relief="groove", bg='pink',fg='black',command=show3)
btnSub = Button(root,text='-',width=5, height=2, relief="groove", bg='pink',fg='black',command=showSub)
btnEqual = Button(root,text='=',width=5, height=2, relief="groove", bg='pink',fg='black',command=showEqual)
btnRadical = Button(root,text='√',width=5, height=2, relief="groove", bg='pink',fg='black')

btn0 = Button(root,text='0',width=5, height=2, relief="groove", bg='pink',fg='black',command=show0)
btnDot = Button(root,text='.',width=5, height=2, relief="groove", bg='pink',fg='black',command=showDot)
btnAdd = Button(root,text='+',width=5, height=2, relief="groove", bg='pink',fg='black',command=showAdd)
btnSin = Button(root,text='sin',width=5, height=2, relief="groove", bg='pink',fg='black')
btnCos = Button(root,text='cos',width=5, height=2, relief="groove", bg='pink',fg='black')
btnModulus = Button(root,text='%',width=5, height=2, relief="groove", bg='pink',fg='black')

label.grid(row=0,column=0,columnspan=5,sticky=NSEW)

btn7.grid(row=1,column=0,sticky=NSEW)
btn8.grid(row=1,column=1,sticky=NSEW)
btn9.grid(row=1,column=2,sticky=NSEW)
btnDiv.grid(row=1,column=3,sticky=NSEW)
btnBack.grid(row=1,column=4,sticky=NSEW)
btnSquare.grid(row=1,column=5,sticky=NSEW)

btn4.grid(row=2,column=0,sticky=NSEW)
btn5.grid(row=2,column=1,sticky=NSEW)
btn6.grid(row=2,column=2,sticky=NSEW)
btnMu.grid(row=2,column=3,sticky=NSEW)
btnClear.grid(row=2,column=4,sticky=NSEW)
btnCube.grid(row=2,column=5,sticky=NSEW)

btn1.grid(row=3,column=0,sticky=NSEW)
btn2.grid(row=3,column=1,sticky=NSEW)
btn3.grid(row=3,column=2,sticky=NSEW)
btnSub.grid(row=3,column=3,sticky=NSEW)
btnEqual.grid(row=3,column=4,sticky=NSEW)
btnRadical.grid(row=3,column=5,sticky=NSEW)

btn0.grid(row=4,column=0,sticky=NSEW)
btnDot.grid(row=4,column=1,sticky=NSEW)
btnAdd.grid(row=4,column=2,sticky=NSEW)
btnSin.grid(row=4,column=3,sticky=NSEW)
btnCos.grid(row=4,column=4,sticky=NSEW)
btnModulus.grid(row=4,column=5,sticky=NSEW)

root.mainloop()